package com.baofu.paycore.biz.engine.processor.impl;

import com.baofu.account.service.facade.model.request.TransFerReqDTO;
import com.baofu.paycore.biz.engine.command.impl.RefundBalCommand;
import com.baofu.paycore.biz.engine.processor.BaseProcessor;
import com.baofu.paycore.biz.engine.result.CommandResult;
import com.baofu.paycore.biz.enums.CommandProcessorEnum;
import com.baofu.paycore.biz.external.account.AccountRefundService;
import com.baofu.paycore.biz.external.account.converter.AccountRequestConverter;
import com.baofu.paycore.biz.external.account.models.AccountFundResBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 余额退款处理器
 * <p>
 * </p>
 * User: kwl Date: 2016/6/14 ProjectName: paycore Version: 5.0.0
 */
@Slf4j
@Service
public class BalanceRefundProcessor implements BaseProcessor<RefundBalCommand> {

    /**
     * 账务退款服务
     */
    @Autowired
    private AccountRefundService accountRefundService;

    /**
     * 获取处理器类型
     *
     * @return    处理器类型
     */
    @Override
    public CommandProcessorEnum getProcessorType() {
        return CommandProcessorEnum.BALANCE_REFUND_PROCESSOR;
    }

    /**
     * 余额退款,走清分退款或提现流程
     *
     * @param  refundBalCommand    退款支付命令
     * @return Map
     */
    @Override
    public Map<String, CommandResult> doProcess(RefundBalCommand refundBalCommand) {

        log.debug("余额退款处理器处理流程, parameter:{}",refundBalCommand);

        //1、指令转换成账务请求对象
        TransFerReqDTO transFerReqDTO = AccountRequestConverter.convert2TransferDTO(refundBalCommand);

        //2、调用账务退款
        AccountFundResBO accountFundResBO = accountRefundService.refund(refundBalCommand.getPayNo(),
                refundBalCommand.getServiceCode(),transFerReqDTO);

        //3、指令返回对象封装
        Map<String, CommandResult> result = new HashMap<>();
        CommandResult commandResult = new CommandResult();
        commandResult.setResponseNo(accountFundResBO.getResponseNo());
        commandResult.setResultCode(accountFundResBO.getErrorCode());
        commandResult.setResultMsg(accountFundResBO.getErrorMsg());
        commandResult.setResponseDate(accountFundResBO.getResponseDate());
        commandResult.setCommand(refundBalCommand);
        commandResult.setRealRtnFlag(true);
        commandResult.setStatus(accountFundResBO.getPayStat());
        result.put(refundBalCommand.getCommandNo(),commandResult);

        return result;
    }

    /**
     * 执行取消方法
     *
     * @param balanceCommands    支付命令
     * @return map               指令执行结果
     */
    @Override
    public Map<String, CommandResult> doCancel(List<RefundBalCommand> balanceCommands) {return null;}
}
